﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using PlanProd.Contracts.enums;
using System.Collections;
using PlanProd.Contracts.Search;

namespace WebAppPlanProduccion.Code
{
    public class UIHelper
    {
        public WebServiceHelper WebServiceHelper { get; set; }

        public int companyId { get; set; }

        public const string cboNullValue = "-1";        

        public static int? GetIntFromInputText(string text)
        {
            if (String.IsNullOrEmpty(text))
            {
                return null;
            }
            else
            {
                return Convert.ToInt32(text);
            }
        }

        public static int? GetIntFromInputCbo(DropDownList cbo)
        {
            if (cbo.SelectedIndex >= 0)
            {
                if (cbo.SelectedValue == cboNullValue)
                    return null;
                else
                    return Convert.ToInt32(cbo.SelectedValue);
            }
            else
            {
                return null;
            }                           
        }

        public static char? GetCharFromInputCbo(DropDownList cbo)
        {
            if (cbo.SelectedIndex >= 0)
            {
                if (cbo.SelectedValue == cboNullValue)
                    return null;
                else
                    return Convert.ToChar(cbo.SelectedValue);
            }
            else
            {
                return null;
            }
        }
       

        public static DateTime? GetDateTimeFromInputText(string text)
        {
            if(String.IsNullOrEmpty(text))
            {
                return null;
            }
            else
            {
                DateTime result;
                bool isParsed = DateTime.TryParseExact(text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None , out result);
                return isParsed ? (DateTime?)result : null;
            }
        }

        public void LoadDestinos(DropDownList cbo, string extraCode = "")
        {                       
            LoadCbo(WebServiceHelper.GetDestinos(companyId, string.Empty), cbo, extraCode);       
        }       

        public void LoadTemporadas(DropDownList cbo, string extraCode = "")
        {                       
            LoadCbo(WebServiceHelper.GetTemporadas(companyId, string.Empty, null, null, true), cbo, extraCode);
        }

        public void LoadCalidad(DropDownList cbo, string extraCode = "")
        {
            List<KeyValuePair<int, string>> calidades = new List<KeyValuePair<int, string>>();
            calidades.Add(new KeyValuePair<int,string>(1, "Primera"));
            calidades.Add(new KeyValuePair<int,string>(2, "Segunda"));
            LoadCbo(calidades, cbo, extraCode, "Key", "Value");
        }

        public static void LoadCbo(IList dataSource, DropDownList cbo, string extraCode, string key = "Id", string value = "Nombre")
        {
            if (!String.IsNullOrEmpty(extraCode))
            {
                cbo.Items.Add(new ListItem(extraCode, cboNullValue));
            }

            string id = string.Empty;
            string name = string.Empty;
            foreach (var dataSourceObject in dataSource)
            {
                Type type = dataSourceObject.GetType();
                id = type.GetProperty(key).GetValue(dataSourceObject, null).ToString();
                name = type.GetProperty(value).GetValue(dataSourceObject, null).ToString();
                cbo.Items.Add(new ListItem(name, id));
            }
        }

        public void LoadCausaDeDesvio(DropDownList cbo, string extraCode = "")
        {                       
            LoadCbo(WebServiceHelper.GetCausasDeDesvio(companyId, null), cbo, extraCode);      
        }

        public void LoadProveedores(DropDownList cbo, string extraCode = "")
        {         
            var proveedores = WebServiceHelper.GetProveedores(WebServiceHelper.ServiceOptionsDefault, new SearchProveedorCriteria() { EmpresaId = companyId});
            LoadCbo(proveedores, cbo, extraCode, value: "RazonSocial");         
        }

        public void LoadTipoDesvio(DropDownList cbo, string extraCode = "")
        {
            var enumValues = Enum.GetValues(typeof(TipoDesvio));
            List<KeyValuePair<int, string>> estados = new List<KeyValuePair<int, string>>();

            foreach (var enumValue in enumValues)
            {
                estados.Add(new KeyValuePair<int, string>((int)enumValue, ((TipoDesvio)(int)enumValue).GetDescription()));
            }

            LoadCbo(estados, cbo, extraCode, "Key", "Value");          
        }

        public void LoadEstadoAprobado(DropDownList cbo, string extraCode = "")
        {            
            var enumValues = Enum.GetValues(typeof(EstadoAprobado));            
            List<KeyValuePair<int, string>> estados = new List<KeyValuePair<int,string>>();

            foreach(var enumValue in enumValues)
            {
                estados.Add(new KeyValuePair<int, string>((int)enumValue, ((EstadoAprobado)(int)enumValue).GetDescription()));
            }

            LoadCbo(estados, cbo, extraCode, "Key", "Value");
        }

        public void LoadEstadoCharAprobado(DropDownList cbo, string extraCode = "")
        {
            var enumValues = Enum.GetValues(typeof(EstadoCharAprobado));
            List<KeyValuePair<int, string>> estados = new List<KeyValuePair<int, string>>();

            foreach (var enumValue in enumValues)
            {
                estados.Add(new KeyValuePair<int, string>((int)enumValue, ((EstadoCharAprobado)enumValue).GetDescription()));
            }

            LoadCbo(estados, cbo, extraCode, "Key", "Value");
        }       
    }
}